package com.example.aloy.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.LinearLayout
import androidx.annotation.ColorInt
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.example.aloy.R

private const val ID_ACTION_BAR = 11
private const val ID_CONTENT_VIEW = 12

abstract class BasicActivity : AppCompatActivity() {

    abstract fun getContentView(): View

    abstract fun getCustomizedActionBarID(): Int

    abstract fun initView()

    @SuppressLint("InflateParams")
    open fun getCustomizedActionBar(): View {
        return LayoutInflater.from(this).inflate(getCustomizedActionBarID(), null)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        //添加自定义标题栏
        val rootView: LinearLayout = LinearLayout(this).apply {
            layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
            )
            setBackgroundColor(ContextCompat.getColor(context, R.color.transparent))
            orientation = LinearLayout.VERTICAL
        }
        rootView.addView(getCustomizedActionBar().apply { id = ID_ACTION_BAR })
        rootView.addView(getContentView().apply { id = ID_CONTENT_VIEW })
        setContentView(rootView)

        initView()
    }

    fun setStatusBarColor(@ColorInt colorInt: Int) {
        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        //设置状态栏颜色
        window.statusBarColor = colorInt
    }

    fun setActionBarColor(@ColorInt colorInt: Int) {
        window.decorView.rootView.findViewById<View>(ID_ACTION_BAR).run {
            setBackgroundColor(colorInt)
        }
    }
}